﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Gmantis.Controls.Extended
{
    public class SlidePanel : ContentControl
    {
        public static readonly DependencyProperty ExpandDirectionProperty =
            DependencyProperty.Register("ExpandDirection", typeof(ExpandDirection), typeof(SlidePanel), new PropertyMetadata(ExpandDirection.Down, OnExpandDirectionPropertyChanged));
        public static readonly DependencyProperty SlideProperty =
            DependencyProperty.Register("Slide", typeof(double), typeof(SlidePanel), new PropertyMetadata(0.0, OnSlidePropertyChanged));

        public ExpandDirection ExpandDirection
        {
            get { return (ExpandDirection)GetValue(ExpandDirectionProperty); }
            set { base.SetValue(ExpandDirectionProperty, value); }
        }

        private static void OnExpandDirectionPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as SlidePanel).InvalidateMeasure();
        }

        public double Slide
        {
            get { return (double)GetValue(SlideProperty); }
            set { SetValue(SlideProperty, value); }
        }

        private static void OnSlidePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as SlidePanel).InvalidateMeasure();
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            var size = base.MeasureOverride(availableSize);
            return new Size(size.Width * (ExpandDirection == Extended.ExpandDirection.Left || ExpandDirection == Extended.ExpandDirection.Right ? Slide : 1),
                size.Height * (ExpandDirection == Extended.ExpandDirection.Up || ExpandDirection == Extended.ExpandDirection.Down ? Slide : 1));
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            finalSize = base.ArrangeOverride(finalSize);

            Clip = new RectangleGeometry { Rect = new Rect(0, 0, finalSize.Width, finalSize.Height) };

            return finalSize;
        }
    }
}
